package cn.bugstack.springframework.beans.factory.support;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import cn.bugstack.springframework.BeanException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;

/**
 * description: jdk实例化具体类
 * taskId：
 * <pre>
 * change history:
 * date             defect#             person                comments
 * ---------------------------------------------------------------------
 * 2024/12/31       ********         zhaochaoyue        create file
 * </pre>
 *
 * @author: zhaochaoyue
 * @date: 2024/12/31 16:35
 * @copyright: 2024
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy{
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args)
        throws BeanException {
        Class clazz = beanDefinition.getBeanClass();
        try {
        if(null == ctor){
            return clazz.getDeclaredConstructor().newInstance();
        }else {
            return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance();
        }
        
        } catch (InstantiationException |IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new BeanException("failed  to instantiate【"+clazz.getName()+"】",e);
        }
    }
}
